package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.User;
import com.coin.member.dto.UserDto;
import com.coin.model.*;
import com.coin.service.UserService;
import com.coin.vo.UseAuthInfoVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * @Auther: 李 力
 * @Date: 2025/6/7
 * @Description: com.coin.controller
 * @version: 1.0
 */
@Api(tags = "会员的控制器")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @Description: 查询会员的列表
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return:
     */
    @GetMapping
    @ApiOperation(value = "查询会员的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "mobile", value = "会员的手机号"),
            @ApiImplicitParam(name = "userId", value = "会员的Id,精确查询"),
            @ApiImplicitParam(name = "userName", value = "会员的名称"),
            @ApiImplicitParam(name = "realName", value = "会员的真实名称"),
            @ApiImplicitParam(name = "status", value = "会员的状态")

    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<User>> findByPage(@ApiIgnore Page<User> page,
                                    String mobile,
                                    Long userId,
                                    String userName,
                                    String realName,
                                    Integer status
    ) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<User> userPage = userService.findByPage(page, mobile, userId, userName, realName, status);
        return R.ok(userPage);
    }


    /**
     * @Description: 修改用户的状态
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @PutMapping("/status")
    @ApiOperation(value = "修改用户的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员的id"),
            @ApiImplicitParam(name = "status", value = "会员的状态"),
    })
    @PreAuthorize("hasAuthority('user_update')")
    public R updateStatus(Long id, Byte status) {
        boolean result = userService.updateStatus(id, status);
        return result ? R.ok() : R.fail("更新失败");
    }

    /**
     * @Description: 修改用户
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param: user
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "会员的json数据"),
    })
    @PreAuthorize("hasAuthority('user_update')")
    public R updateStatus(@RequestBody @Validated User user) {
        boolean result = userService.updateUser(user);
        return result ? R.ok() : R.fail("修改失败");
    }

    /**
     * @Description: 获取用户的详情
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param: id
     * @return: R
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取用户的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的Id")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<User> userInfo(Long id) {
        User user = userService.getUserInfo(id);
        return R.ok(user);
    }

    /**
     * @Description: 查询该用户邀请的用户列表
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @GetMapping("/directInvites")
    @ApiOperation(value = "查询该用户邀请的用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "该用户的Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),

    })
    public R<Page<User>> getDirectInvites(@ApiIgnore Page<User> page, Long userId) {
        Page<User> userPage = userService.findDirectInvitePage(page, userId);
        return R.ok(userPage);
    }

    /**
     * @Description: 查询用户的审核列表
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @GetMapping("/auths")
    @ApiOperation(value = "查询用户的审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "mobile", value = "会员的手机号"),
            @ApiImplicitParam(name = "userId", value = "会员的Id,精确查询"),
            @ApiImplicitParam(name = "userName", value = "会员的名称"),
            @ApiImplicitParam(name = "realName", value = "会员的真实名称"),
            @ApiImplicitParam(name = "reviewsStatus", value = "会员的状态")

    })
    public R<Page<User>> findUserAuths(
            @ApiIgnore Page<User> page,
            String mobile,
            Long userId,
            String userName,
            String realName,
            Integer reviewsStatus
    ) {
        Page<User> userPage = userService.findUserAuths(page, mobile, userId, userName, realName, reviewsStatus);
        return R.ok(userPage);
    }

    /**
     * @Description: 查询用户的认证详情
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @GetMapping("/auth/info")
    @ApiOperation(value = "查询用户的认证详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的Id")
    })
    public R<UseAuthInfoVo> getUseAuthInfo(Long id) {
        UseAuthInfoVo useAuthInfoVo = userService.getUseAuthInfo(id);
        return R.ok(useAuthInfoVo);
    }

    /**
     * @Description: 审核
     * @Author: 李 力
     * @Date: 2025/6/9
     * @Param:
     * @return: R
     */
    @PostMapping("/auths/status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的ID"),
            @ApiImplicitParam(name = "authStatus", value = "用户的审核状态"),
            @ApiImplicitParam(name = "authCode", value = "一组图片的唯一标识"),
            @ApiImplicitParam(name = "remark", value = "审核拒绝的原因"),
    })
    public R updateUserAuthStatus(@RequestParam(required = true) Long id,
                                  @RequestParam(required = true) Byte authStatus,
                                  @RequestParam(required = true) Long authCode,
                                  @RequestParam(required = false) String remark) {
        boolean result = userService.updateUserAuthStatus(id, authStatus, authCode, remark);
        return result ? R.ok() : R.fail("审核失败");
    }

    /**
     * @Description: 获取当前登录用户的详情
     * @Author: 李 力
     * @Date: 2025/6/10
     * @Param:
     * @return: R
     */
    @GetMapping("/current/info")
    @ApiOperation(value = "获取当前登录用户的详情")
    public R<User> currentUserInfo() {
        // 获取用户的Id
        String idStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        User user = userService.getCurrentUserInfo(Long.valueOf(idStr));
        return R.ok(user);
    }

    /**
     * @Description: 实名身份认证
     * @Author: 李 力
     * @Date: 2025/6/10
     * @Param:
     * @return: R
     */
    @GetMapping("/authAccount")
    public R check(@RequestBody UserAuthForm userAuthForm) {
        String idStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        boolean result = userService.infoCheck(Long.valueOf(idStr), userAuthForm);
        return result ? R.ok(true, "认证通过") : R.fail(false, "认证不通过");
    }

    /**
     * @Description: 修改手机号码
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param:
     * @return: R
     */
    @PostMapping("/updatePhone")
    @ApiOperation(value = "修改手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updatePhoneParam", value = "updatePhoneParam 的json数据")
    })
    public R updatePhone(@RequestBody UpdatePhoneParam updatePhoneParam) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        boolean result = userService.updatePhone(Long.valueOf(userId), updatePhoneParam);
        return result ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    /**
     * @Description: 修改登录密码
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param: updateLoginParam
     * @return: R
     */
    @PutMapping("/updateLoginPassword")
    public R updateLoginPwd(@RequestBody updateLoginParam updateLoginParam) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        boolean result = userService.updateLoginPwd(Long.valueOf(userId), updateLoginParam);
        return result ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    /**
     * @Description: 修改交易密码
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param: updateLoginParam
     * @return: R
     */
    @PutMapping("/updatePayPassword")
    public R updatePayPwd(@RequestBody updateLoginParam updateLoginParam) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        boolean result = userService.updatePayPwd(Long.valueOf(userId), updateLoginParam);
        return result ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    /**
     * @Description: 给管理服务提供远程调用
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param: ids
     * @return: R
     */
    @GetMapping("/basic/users")
    List<UserDto> getBasicUsers(@RequestParam("ids") List<Long> ids) {
        return userService.getBasicUsers(ids);
    }

    /**
     * @Description: 用户的注册
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param:
     * @return: R
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户的注册")
    public R register(@RequestBody RegisterParam registerParam) {
        boolean isOk = userService.register(registerParam);
        return isOk ? R.ok(true, "注册成功") : R.fail("注册失败");
    }

    /**
     * @Description: 给财务系统提供远程调用
     * @Author: 李 力
     * @Date: 2025/6/13
     * @Param:
     * @return: R
     */
    @GetMapping("/basic/users2")
    public Map<Long, UserDto> getBasicUsers2(@RequestParam(value = "ids", required = false) List<Long> ids,
                                             @RequestParam(value = "userName", required = false) String userName,
                                             @RequestParam(value = "mobile", required = false) String mobile
    ) {
        return userService.getBasicUsers2(ids, userName, mobile);
    }
}
